package com.example.service;

import com.example.config.MqttInboundConfiguration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * mqtt消息订阅
 *
 * @author ysb
 * @version V1.0
 * @Date 2024/11/12
 **/
@Component
public class MqttMessageSubscriber {

    @Resource
    private MqttInboundConfiguration mqttInboundConfiguration;


    public String subscribeTopic(String topic) {
        try {
            mqttInboundConfiguration.addSubscription(topic, 1);
            return "Subscribed to topic: " + topic;
        } catch (Exception e) {
            e.printStackTrace();
            return "Failed to subscribe to topic: " + topic;
        }
    }

    public String subscribeTopic(String topic, Integer qos) {
        if (null == qos) {
            qos = 1;
        }
        try {
            mqttInboundConfiguration.addSubscription(topic, qos);
            return "Subscribed to topic: " + topic;
        } catch (Exception e) {
            e.printStackTrace();
            return "Failed to subscribe to topic: " + topic;
        }
    }
}
